﻿
Imports System.IO
Imports std
Imports xmlctrl
Imports xhtmlctrl

Public Class googlemap
    Inherits div

    Public Const baseUrl As String = "http://maps.google.com/maps/api/staticmap"
    Public Const defaultErrorText As String = "??LOCATION IS NOT SET??"

    Private Class parameters
        Public Const location As String = "location"
        Public Const zoom As String = "zoom"
        Public Const width As String = "width"
        Public Const height As String = "height"
        Public Const maptype As String = "maptype"
        Public Const label As String = "label"
        Public Const labelColor As String = "labelColor"
    End Class

    Private Class parameters_values
        Public location As String = Nothing
        Public zoom As Int32 = 18
        Public width As Int32 = 300
        Public height As Int32 = 300
        Public maptype As String = "roadmap"
        Public label As String = "D"
        Public labelColor As String = "blue"
    End Class
    Private pv As parameters_values = Nothing

    Private Function parseQuery(ByVal q As String) As Boolean
        Dim words As vector(Of String) = Nothing
        strsepar(q, Global.defaultValue.spaceChars, words, character.quoteMark)
        For i As Int64 = 0 To words.size() - 1
            If strsame(words(i), parameters.location, False) Then
                pv.location = words(i + 1)
            ElseIf strsame(words(i), parameters.zoom, False) Then
                convertor.toint32(words(i + 1), pv.zoom)
            ElseIf strsame(words(i), parameters.width, False) Then
                convertor.toint32(words(i + 1), pv.width)
            ElseIf strsame(words(i), parameters.height, False) Then
                convertor.toint32(words(i + 1), pv.height)
            ElseIf strsame(words(i), parameters.maptype, False) Then
                pv.maptype = words(i + 1)
            ElseIf strsame(words(i), parameters.label, False) Then
                pv.label = words(i + 1)(0)
            ElseIf strsame(words(i), parameters.labelColor, False) Then
                pv.labelColor = words(i + 1)
            Else
                pv.location = words(i)
                i -= 1
            End If
            i += 1
        Next

        Return Not isemptystring(pv.location)
    End Function

    Private Function generateMarker() As String
        Return "label:" + pv.label + "|color:" + pv.labelColor + "|" + pv.location
    End Function

    Private Function generateSize() As String
        Return Convert.ToString(pv.width) + "x" + Convert.ToString(pv.height)
    End Function

    Private Function generateStaticMapUrl() As String
        Return urlUtils.appendQuery(baseUrl, _
                                    "markers", generateMarker(), _
                                    "zoom", Convert.ToString(pv.zoom), _
                                    "size", generateSize(), _
                                    "maptype", pv.maptype, _
                                    "sensor", "false")
    End Function

    Public Sub New(ByVal q As String, _
                   Optional ByVal errorText As String = defaultErrorText, _
                   Optional ByVal buff As Stream = Nothing)
        If isemptystring(errorText) Then
            errorText = defaultErrorText
        End If
        pv = New parameters_values()
        If parseQuery(q) Then
            Dim img As img = Nothing
            img = New img(buff)
            img.src = generateStaticMapUrl()
            addchild(img)
            Dim div As div = Nothing
            div = New div(buff)
            div.text = pv.location
            addchild(div)
            If isdebugmode() Then
                div = New div(buff)
                div.text = q
                addchild(div)
            End If
        Else
            Dim div As div = Nothing
            div = New div(buff)
            div.text = errorText
            addchild(div)
        End If
    End Sub
End Class
